import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";
// import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   name: 'home',
  //   component: HomeView
  // },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
  {
    path: '/',
    name: '/',
    component: () => import('../views/index.vue'),
    redirect: { path: "/home" },
    children: [
      {
        path: "home",
        name: "home",
        component: (resovle) => require(["../views/home.vue"], resovle),
      },
      {
        path: "map",
        name: "map",
        component: (resovle) => require(["../views/map.vue"], resovle),
      },
      {
        path: "mapList",
        name: "mapList",
        component: (resovle) => require(["../views/mapList.vue"], resovle),
      },
      {
        path: "mapMG",
        name: "mapMG",
        component: (resovle) => require(["../views/mapMG.vue"], resovle),
      },
      {
        path: "userMG",
        name: "userMG",
        component: (resovle) => require(["../views/userMG.vue"], resovle),
      },
      {
        path: "person",
        name: "person",
        component: (resovle) => require(["../views/person.vue"], resovle),
      },
    ],
  },
  {
    path: '/Login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 针对ElementUI导航栏中重复导航报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


// // 提供一个重置路由的方法
// export const resetRouter = () => {
//   router.matcher = new VueRouter({
//     mode: 'history',
//     base: process.env.BASE_URL,
//     routes
//   })
// }

// // 注意：刷新页面会导致页面路由重置
// export const setRoutes = () => {
//   const storeMenus = localStorage.getItem("menus");
//   if (storeMenus) {

//     // 获取当前的路由对象名称数组
//     const currentRouteNames = router.getRoutes().map(v => v.name)
//     if (!currentRouteNames.includes('Manage')) {
//       // 拼装动态路由
//       const manageRoute = { path: '/', name: 'Manage', component: () => import('../views/Manage.vue'), redirect: "/home", children: [
//           { path: 'person', name: '个人信息', component: () => import('../views/person.vue')},
//           { path: 'password', name: '修改密码', component: () => import('../views/password.vue')}
//         ] }
//       const menus = JSON.parse(storeMenus)
//       menus.forEach(item => {
//         if (item.path) {  // 当且仅当path不为空的时候才去设置路由
//           let itemMenu = { path: item.path.replace("/", ""), name: item.name, component: () => import('../views/' + item.pagePath + '.vue')}
//           manageRoute.children.push(itemMenu)
//         } else if(item.children.length) {
//           item.children.forEach(item => {
//             if (item.path) {
//               let itemMenu = { path: item.path.replace("/", ""), name: item.name, component: () => import('../views/' + item.pagePath + '.vue')}
//               manageRoute.children.push(itemMenu)
//             }
//           })
//         }
//       })
//       // 动态添加到现在的路由对象中去
//       router.addRoute(manageRoute)
//     }

//   }
// }

// // 重置我就再set一次路由
// setRoutes()


// router.beforeEach((to, from, next) => {
//   localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称
//   store.commit("setPath")

//   // 未找到路由的情况
//   if (!to.matched.length) {
//     const storeMenus = localStorage.getItem("menus")
//     if (storeMenus) {
//       next("/404")
//     } else {
//       // 跳回登录页面
//       next("/login")
//     }
//   }
//   // 其他的情况都放行
//   next()

// })
export default router
